package cn.wolfcode.controller;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//声明这是一个名字叫做Test的控制器
@RestController
@CrossOrigin//允许跨域
public class TestController {

    @Value("${deepseek.api}")//读取配置文件中的deepseek.Api变量中的值，并将值放到deepseekApi变量中
    private String deepseekApi;

    @RequestMapping("/time")
    public String time(){
        return new Date().toLocaleString();
    }

    @RequestMapping("/getAnswer")
    public String getAnswer(String keywords, HttpServletRequest request){

        //判断是否有当前用户（判断是否已登录)
        String ip = request.getRemoteAddr();
        Object currentUser = EmployeeController.map.get(ip);
        if (currentUser == null) {//未登录
            return "请登录后查询";
        }

        String question = "请用50字以内描述"+keywords;
        ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("https://api.deepseek.com/v1")
                .apiKey(deepseekApi)
                .modelName("deepseek-chat")
                .temperature(0.7)
                .build();
        String answer = model.generate(question);

        return keywords+"问题的答案是："+answer;
    }
}
